package com.hrms.doctor.service.impl;

import com.hrms.doctor.domain.Message;
import com.hrms.doctor.domain.vo.MessageVo;
import com.hrms.doctor.mapper.MessageMapper;
import com.hrms.doctor.service.MessageService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.Duration;
import java.time.LocalDateTime;
import java.util.*;

import static java.lang.Math.abs;

@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    MessageMapper messageMapper;
    @Override
    public Integer insertDoctorMessage(Message message){
        message.setStatus((short)1);
        message.setCreateTime(LocalDateTime.now());
        return messageMapper.insertDoctorMessage(message);
    }

    @Override
    public Integer insertPatientMessage(Message message) {
        message.setStatus((short)1);
        message.setCreateTime(LocalDateTime.now());
        return messageMapper.insertPatientMessage(message);
    }




    @Override
    public List<Message> selectMessage(Long doctorId,Long patientId,int opt){
        if(opt==1) {
            List<Message> rs = messageMapper.selectMessage(doctorId, patientId);
            rs.forEach(p -> {
                update(p);
            });
            return rs;
        }else if(opt==2){
            List<Message> rs = messageMapper.selectMessage(doctorId, patientId);
            return rs;
        }else if(opt==3){
            List<Message> rs = messageMapper.selectMessageInPatient(doctorId, patientId);
            rs.forEach(p -> {
                updatePatient(p);
            });
            return rs;
        }else if(opt==4){
            List<Message> rs = messageMapper.selectMessageInPatient(doctorId, patientId);
            return rs;
        }
    return null;
    }

    @Override
    public Integer delete(Long doctorId, Long patientId, LocalDateTime createTime){

        Duration duration=Duration.between(LocalDateTime.now(),createTime);
        Long minutes=duration.toMinutes();
        //System.out.printf("%s%d","时间是",minutes);
        if(abs(minutes)>2)return 0;
        else return messageMapper.delete(doctorId,patientId,createTime);
    }
    @Override
    public void update(Message message){
        messageMapper.update(message);
    }
    @Override
    public void updatePatient(Message message){
        messageMapper.updatePatient(message);
    }
    @Override
    public Integer countPatient(){
        return messageMapper.countPatient();
    }
    @Override
    public Integer countDoctor(){
        return messageMapper.countDoctor();
    }

    @Override
    public List<MessageVo> getMessage(Long doctorId, Long patientId) {

        List<Message> doctorMessage=messageMapper.getDoctor(doctorId,patientId);
        List<Message> patientMessage=messageMapper.getPatient(doctorId,patientId);
        List<MessageVo> messageVos=new ArrayList<>();
        for(int i=0;i<doctorMessage.size();i++){
            messageVos.add(new MessageVo((short)0,doctorMessage.get(i).getContent(),doctorMessage.get(i).getCreateTime()));
        }
        for(int i=0;i<patientMessage.size();i++){
            messageVos.add(new MessageVo((short)1,patientMessage.get(i).getContent(),patientMessage.get(i).getCreateTime()));
        }
        Collections.sort(messageVos, new Comparator<MessageVo>() {
            @Override
            public int compare(MessageVo o1, MessageVo o2) {
                return o2.getCreateTime().compareTo(o1.getCreateTime());
            }
        });

       return messageVos;
    }


}
